var req;
var requester;
var app;
function Login() {
  login=document.getElementById('wlogin').value;
  if (login=='') alert('Login can\'t be empty');
  else {
    password=document.getElementById('wpass').value
    if (password=='') alert('Password can\'t be empty');
    else {
      requester='ur';
      url='/cgi-bin/widesktop/plogin.py?login='+login+'&password='+password;
      loadXMLDoc(url,0);
    }
  }
}
function Register() {
  login=document.getElementById('wlogin').value;
  password=document.getElementById('wpass').value;
  rpassword = document.getElementById('wrpass').value;
  fname=document.getElementById('wfname').value;
  lname=document.getElementById('wlname').value;
  if (login=='') alert('Login can\'t be empty');
  else {
  if (password=='') alert ('Password can\'t be empty');
  else {
  if (rpassword!=password) alert('Passwords do not match');
  else {
  if (fname=='') alert('Please fill the first name');
  else {
  if (lname=='') alert('Please fill the last name');
  else {
    requester='desktop';
    url='/cgi-bin/widesktop/pregister.py?login='+login+'&password='+password+'&fname='+fname+'&lname='+lname;
    loadXMLDoc(url,1);
  }}}}}
}
function NewWindow(wtitle,inner) {
  document.getElementById('desktop').innerHTML='<table border="1" cellpadding="0" cellspacing="0" width="100%"><tr style="background:url(\'images/win.png\') repeat-x;"><td height="24"><span class="win-header">'+wtitle+'</span></td></tr><tr><td style="background:url(\'images/trans.png\') repeat-x;" align="center">'+
  inner+'</td></tr></table>';
}
function ShowDesktop() {
  NewWindow('Applications', '<table class="win-app" border="0" cellpadding="0" cellspacing="0"><tr><td align="center"><a href="javascript:ShowNotes();"><img src="./images/notes.png"><br>Notes</a></td><td align="center"><img src="./images/todo.png"><br>ToDo</td></tr></table>');
}
function ShowLoginForm() {
  NewWindow('Login form', 'Login:<br><form name="wloginform" onsubmit="Login();return false;"><input type="text" id="wlogin" name="login" value=""><br>Password:<br><input id="wpass" type="password" name="password"><br><input type="submit" name="enter" value="Enter"></form>'+
  '<a href="javascript:;" onclick="ShowRegisterForm()">I don\'t have an account</a>');
}
function ShowRegisterForm() {
  NewWindow('Registration form','Login:<br><form name="wloginform" onsubmit="Register();return false;"><input type="text" id="wlogin" name="login" value=""><br>Password:<br><input id="wpass" type="password" name="password"><br>Repeat Password:<br><input id="wrpass" type="password" name="rpassword"><br>First Name:<br><Input type="text" name="wfname" id="wfname"><br>Last Name:<br><input type="text" name="wlname" id="wlname"><br><input type="submit" name="enter" value="Register"></form>');
}
function CheckUser() {
  uid = document.getElementById('uid').value;
  rand = document.getElementById('rand').value;
  url = '/cgi-bin/widesktop/pusercheck.py?uid='+uid+'&rand='+rand;
  loadXMLDoc(url,2);
}

function loadXMLDoc(url, mode) {
 if (window.XMLHttpRequest) {
  req = new XMLHttpRequest();
  if (mode==0) req.onreadystatechange = processLogin;
  if (mode==1) req.onreadystatechange = processCommon;
  if (mode==2) req.onreadystatechange = CheckUserServer;
  if (mode==3) req.onreadystatechange = processApp;
  req.open("GET", url, true);
  req.send(null);
} else if (window.ActiveXObject) {
  req = new ActiveXObject("Microsoft.XMLHTTP");
  if (req) {
    if (mode==0) req.onreadystatechange = processLogin;
    if (mode==1) req.onreadystatechange = processCommon;
    if (mode==2) req.onreadystatechange = CheckUserServer;
    if (mode==3) req.onreadystatechange = processApp;
    req.open("GET", url, true);
    req.send();
  }
 }
}

function CheckUserServer() {
  ab = window.setTimeout("req.abort();", 15000);
  if (req.readyState == 4) {
  clearTimeout(ab);
    if (req.status == 200) {
      is_valid=req.responseText;
      if(is_valid==0) ShowLoginForm(); else ShowDesktop();
    } else {
    }
  }  
}
function processApp() {
  ab = window.setTimeout("req.abort();", 15000);
  if (req.readyState == 4) {
    clearTimeout(ab);
    if (req.status == 200) {
      NewWindow(app, req.responseText);
    } else {
    }
  }  

}
function processCommon() {
  ab = window.setTimeout("req.abort();", 15000);
  if (req.readyState == 4) {
    clearTimeout(ab);
    if (req.status == 200) {
      document.getElementById(requester).innerHTML=req.responseText;
    } else {
    }
  }  
}
function processLogin() {
  ab = window.setTimeout("req.abort();", 15000);
  if (req.readyState == 4) {
    clearTimeout(ab);
    if (req.status == 200) {
      document.getElementById(requester).innerHTML=req.responseText;
      CheckUser();
    } else {
    }
  }  
}